/*
	StargateWW for GarrysMod10
	Copyright (C) 2008 Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*	Notice, this is all a mess.  I wrote most of the gamemode BEFORE I learned
	a lot about LUA.  During coding this gamemode, I have learned much more than
	I had.  I've attempted to reconvert old functions to the better way and newer
	way, but some things like the giant derma system are too big to rewrite.
	If you are wise, don't look farther.  Your eyes will bleed.
	So, I didn't even bother going through and commenting anything.
*/

DeriveGamemode("sandbox");
include("shared.lua")

local IncludeFiles = {"chatbox", "death", "dermautils", "disconnected", "hud", "shakeview", "weather", "utils"}
for k,v in pairs(IncludeFiles) do
	include("cl/cl_"..v..".lua")
end

local IncludeFiles = {"extentions", "flavor", "chatcommands", "upgrademanager"}
for k,v in pairs(IncludeFiles) do
	include("sh/sh_" .. v .. ".lua")
end

require("datastream")

SpeakLoaded = false
if require("speak") then SpeakLoaded = true print("Speak Loaded!") else SpeakLoaded = false end

SGDMVER = WW.Version

ChangedClass = false

FontResize = (ScrW()/1280)

surface.CreateFont("stargate",FontResize*18,400,true,false,"NotifyFont"); -- set fonts
surface.CreateFont("stargate atlantis glyphs", FontResize*18,400,true,false,"DetailFont");
surface.CreateFont("arial", 20, 500, true, false, "NText")

--################# Derma Control @Prefanatic
function infoDerma (um, ply)

	-- Shows the DermaPanel that holds all the derma functions.
	InfoDPanel = vgui.Create("DFrame")
	
	InfoDPanel:SetPos(XPos, 5)
	InfoDPanel:SetSize(470, 600)
	InfoDPanel:SetTitle( "StargateWW " .. SGDMVER )
	InfoDPanel:SetVisible( true )
	InfoDPanel:SetDraggable( true )	
	InfoDPanel:SetBackgroundBlur( true )
	InfoDPanel:ShowCloseButton( true )
	InfoDPanel:MakePopup()

	local PropertySheet = CreatePropertySheet( 0, 30, WW.Derma.StandX, WW.Derma.StandY + 5, InfoDPanel, PropertySheet )
	
	local teamlist = CreateList( 0, 0, WW.Derma.StandX, WW.Derma.StandY, 5, true, nil )
	
	for k,v in pairs(player.GetAll()) do
	
		if LocalPlayer():GetTeamBase() == v:GetTeamBase() and v:Team() != 1001 then
		
			if LocalPlayer():Nick() == v:Nick() then you = "(You)" else you = "" end
			
			local listpanel = CreatePanel( 0, 0, 320, 100, Color( 128, 128, 128, 255 ), nil, listpanel )	
				CreateLabel( 5, 5, v:WWGetClass() .. "  --  " .. v:Nick() .. " " .. you, "NText", listpanel, ClassLabel )
				CreateModelImg( 5, 25, LocalPlayer():WWGetModel(), listpanel, ClassModelImg )
				CreateLabel( 75, 30, "Health: " .. v:Health(), "default", listpanel, HealthLabel )
			
				local dist = math.Round(LocalPlayer():GetPos():Distance(v:GetPos())) .. " units."
				CreateLabel( 75, 40, "Distance: " .. dist, "default", listpanel, DistLabel )
				CreateLabel( 75, 50, "Kills: " .. v:Frags(), "default", listpanel, KillLabel )
				CreateLabel( 75, 60, "Deaths: " .. v:Deaths(), "default", listpanel, DeathLabel )
				
				CreateLabel( 75, 80, "Team: " .. v:GetTeamName(), "NText", listpanel, TeamName )
							
				if LocalPlayer():IsLeader() then CreateLabel( 350, 60, "Credits: " .. v:GetCredits(), "NText", listpanel, CredLabel ) end
			
				if LocalPlayer():IsAdmin() then CreateLabel( 350, 20, "Admin", "NText", listpanel, AdminLabel ) end
				
				CreateLabel( 260, 85, "PM", "default", listpanel, P2PLabel )
				local p2pentry = CreateTextEntry( 280, 80, 150, 20, true, nil, listpanel, NotifEntry )
				p2pentry.OnEnter = function() v:Ply2Ply( LocalPlayer(), p2pentry:GetValue() ) 
												p2pentry:SetText("Message Sent!") 
												timer.Simple(2, function() p2pentry:SetText("") end) 
												LocalPlayer():PlaySound(WW.Sounds.Press) 
									end
				
				teamlist:AddItem(listpanel)
				
			
		end
	end
	
	if LocalPlayer():IsLeader() then
	
		local Upgrades = CreatePanel( 0, 0, 470, 560, Color( 50, 50, 50, 255 ), nil, Upgrades )
		local PLStyle = CreatePanel( 20, 20, 110, 150, Color( 150, 150, 150, 255 ), Upgrades, PLStyle )
		CreateLabel( "center", 10, "Players", "NText", PLStyle, PLLabel )
		
		local PIStyle = CreatePanel( 150, 20, 270, 310, Color( 150, 150, 150, 255 ), Upgrades, PIStyle )
		local PINameL = CreateLabel( "center", 10, "Please select a player to upgrade", "NText", PIStyle, PINameL )
		local PIModelI = CreateModelPanel( 50, 10, 300, 300, "", true, 90, PIStyle, PIModelI )
		local PIHealthL = CreateLabel( 5, 40, "Health will be shown when player is selected.", "default", PIStyle, PIHealthL )
		local PIUpgraL = CreateLabel( 5, 55, "Upgrades will be shown when player is selected.", "default", PIStyle, PIUpgraL )
		
		local PIPlyList = CreateLabel( "center", 30, "", "default", PIStyle, PIPlyList )
		
		local PlayerList = CreateComboBox( 5, 30, 100, 100, PLStyle, PlayerList )
		
			for k,v in pairs(player.GetAll()) do
				if LocalPlayer():GetTeamBase() == v:GetTeamBase() and v:Team() != 1001 then
					PlayerList:AddItem( v:Nick() )
				end
			end
			
			PlayerList:AddItem("Team")

		local SelectLabel = vgui.Create( "DPanel", Upgrades )
		
		local AlreadySearched = false
		local AlreadyAdded = false
		
		SelectLabel:SetPos( 220, 170 )
		SelectLabel:SetSize( 200, 16 )
		SelectLabel.Paint = function()
			if PlayerList:GetSelectedItems() and PlayerList:GetSelectedItems()[1] then 					
				if PlayerList:GetSelectedItems()[1]:GetValue() == "Team" then 
	
					PINameL:SetText("You will upgrade the Team")
					PIModelI:SetVisible(false)
					PIHealthL:SetVisible(false)
					PIUpgraL:SetVisible(false)
					
					local plylist = ""
					
						for k,v in pairs(player.GetAll()) do
						
							if v:GetTeamBase() == LocalPlayer():GetTeamBase() then plylist = plylist .. ", " .. v:Nick() end
							
						end
					
						PIPlyList:SetText(plylist)
						AlreadySearched = true
					
				else
					local ply = PlayerList:GetSelectedItems()[1]:GetValue()
					
					PINameL:SetText("You will upgrade: " .. ply)
					PIHealthL:SetText("Health: " .. GetPlyFromNick( ply ):Health() )
					PIUpgraL:SetText("Upgrades: " )
					
					if !AlreadyAdded then PIModelI:SetModel(GetPlyFromNick( ply ):WWGetModel()) AlreadyAdded = true end
					
					PIModelI:SetVisible(true)
					PIHealthL:SetVisible(true)
					PIUpgraL:SetVisible(true)
					
				end
			end
		end
		
		local UPStyle = CreatePanel( 20, 180, 110, 150, Color( 150, 150, 150, 255 ), Upgrades, UPStyle )
		local UPLabel = CreateLabel( "center", 10, "Upgrades", "NText", UPStyle, UPLabel )
		local UPList = CreateList( 5, 30, 100, 100, 5, true, UPStyle, UPList )
		local HelpStyle = CreatePanel( 15, 350, 430, 180, Color( 150, 150, 150, 255 ), Upgrades, HelpStyle )
		local HelpLabel = CreateLabel( "center", 10, "Upgrade Descriptions: Hover over the button to see!", "NText", HelpStyle, HelpLabel )
		
		for k,v in pairs(upgrades) do
		
			if LocalPlayer():GetTeamBase() != v.TRestriction then 
			
				local help = CreateLabel(10, 30, v.Help, "NText", HelpStyle, help)
					help:SetVisible(false)
				
				local function OnClickFunc()
					local nply = PlayerList:GetSelectedItems()[1]:GetValue()
				
					if PlayerList:GetSelectedItems()[1] and nply == "Team" then isteam = true else isteam = false end
					if PlayerList:GetSelectedItems() and PlayerList:GetSelectedItems()[1] then selected = true else selected = false end
					
					if isteam and selected then CallServerFunction(v.Arg, "", tostring(isteam)) end
					if !isteam and selected then CallServerFunction(v.Arg, nply, tostring(isteam)) end
					LocalPlayer():PlaySound(WW.Sounds.Press)
					InfoDPanel:SetVisible(false)
					
				end
				
				local function HoverEnterFunc()
				
					help:SetVisible(true)
					LocalPlayer():PlaySound(WW.Sounds.Press2)
					
				end
				
				local function HoverExitFunc()
				
					help:SetVisible(false)
					
				end
				
				local button = CreateButton( 0, 0, 40, 20, v.Name, OnClickFunc, HoverEnterFunc, HoverExitFunc, nil, button )
			
				UPList:AddItem(button)
			end
			
		end
		
		PropertySheet:AddSheet( "Upgrades", Upgrades, "gui/silkicons/brick_add", false, false, "Information on the Team" )
	
	end
		
	PropertySheet:AddSheet( "Team Info", teamlist, "gui/silkicons/world", false, false, "Information on the Team" )
	
end

function teamDerma (um, ply)	
	-- Shows the DermaPanel that holds all the derma functions.
	local DermaPanel = vgui.Create("DFrame")
	
	DermaPanel:SetPos(XPos, 5)
	DermaPanel:SetSize(470, 600)
	DermaPanel:SetTitle( "StargateWW " .. SGDMVER )
	DermaPanel:SetVisible( true )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	
	DermaPanel:SetBackgroundBlur( true )
	DermaPanel:MakePopup()
	
	local PropertySheet = CreatePropertySheet( 0, 30, WW.Derma.StandX, WW.Derma.StandY + 5, DermaPanel, PropertySheet )
	
	local JobInfo = CreateList( 0, 0, 470, 560, 5, true, nil, JobInfo )
		
	
	for k,v in pairs(WWClass) do
		if v.Team != 1 then
			if team.NumPlayers(v.Team) != 1 then
							
				local jobpanel = CreatePanel( 0, 0, 320, 100, Color( 128, 128, 128, 255 ), nil, jobpanel )
				
				CreateLabel( 5, 5, v.Class, "NText", jobpanel, LLabel )
				
				CreateModelImg( 5, 25, v.Model, jobpanel, MPic )
				
				CreateLabel( 75, 25, v.Info, "default", jobpanel, InfoL )
				CreateLabel( 350, 20, "Weapons: ", "default", jobpanel, InfoW )
				CreateLabel( 350, 25, GetClassWeapons( v.Team ), "default", jobpanel, InfoWL )
				
				local function DoClickFunc() 
						CallServerFunction("ChangeTeam", v.Team)
						ChangedClass = true
						DermaPanel:SetVisible(false)
						LocalPlayer():PlaySound(WW.Sounds.Press)
				end
				
				local function HoverEnterFunc() LocalPlayer():PlaySound(WW.Sounds.Press2) end
				local function HoverExitFunc() end
				
				CreateButton( 100, 70, 200, 20, "Become", DoClickFunc, HoverEnterFunc, HoverExitFunc, jobpanel, Apply )
				
				JobInfo:AddItem(jobpanel)
				
			end
		end
	end
		
		PropertySheet:AddSheet( "Characters", JobInfo, "gui/silkicons/group", false, false, "To change characters, come here." )
end

function helpDerma (um, ply)

	--RunConsoleCommand("play", WW.Sounds.Help)
-- Shows the DermaPanel that holds all the derma functions.
	starting = um:ReadBool()
	local DermaPanel = vgui.Create("DFrame")
	XPos = (ScrW() / 2) - (470 / 2)
	
	DermaPanel:SetPos(XPos, 40)
	DermaPanel:SetSize(470, 600)
	DermaPanel:SetTitle( "StargateWW " .. SGDMVER )
	DermaPanel:SetVisible( true )
	DermaPanel:SetDraggable( true )
	
	DermaPanel:ShowCloseButton( false )
	
	DermaPanel:SetBackgroundBlur( true )
	DermaPanel:MakePopup()
	
	local PropertySheet = CreatePropertySheet( 0, 30, WW.Derma.StandX, WW.Derma.StandY + 5, DermaPanel, PropertySheet)
	
	local HelpInfo = CreateList( 0, 0, WW.Derma.StandX, WW.Derma.StandY, 5, true, nil, HelpInfo )
	
	local Help = CreateLabel( "center", 5, "Help", "NText", HelpInfo, Help )
		HelpInfo:AddItem( Help )
		
	local HL1 = CreateLabel( 0, 0, GMHelp, "default", HelpInfo, HL1 )
		HelpInfo:AddItem( HL1 )
			
	local function OnClickFunc() if starting then teamDerma(um, ply) end DermaPanel:SetVisible( false ) LocalPlayer():PlaySound(WW.Sounds.Press) end
	local function HoverEnterFunc() LocalPlayer():PlaySound(WW.Sounds.Press2) end
	local function HoverExitFunc() end
		
	CreateButton( "center", 400, 200, 50, "I read the help.", OnClickFunc, HoverEnterFunc, HoverExitFunc, HelpInfo, hl1button )
	
	local Changelog = CreateList( 0, 0, WW.Derma.StandX, WW.Derma.StandY, 5, true, nil, Changelog )
	
	local CL1 = vgui.Create("DLabel")
	
		if AChangeLog then changetext = AChangeLog else changetext = "No changelog found!" end
		
	local CL1 = CreateLabel( 5, 5, changetext, "default", nil, CL1 )
		Changelog:AddItem( CL1 )
	
	-- Set up tabs.
	PropertySheet:AddSheet( "Help", HelpInfo, "gui/silkicons/exclamation", false, false, "Need some help?" )	
	PropertySheet:AddSheet( "ChangeLog", Changelog, "gui/silkicons/table_edit", false, false, "All things done in World Wars" )	
	
end

usermessage.Hook( "infoMenu", infoDerma )
usermessage.Hook( "teamMenu", teamDerma )
usermessage.Hook( "helpMenu", helpDerma )
usermessage.Hook( "upgradeMenu", upgradeDerma )

local matBlurEdges		= Material( "bluredges" )
local tex_MotionBlur	= render.GetMoBlurTex0()

ColorEffects = {}


function ResetEffects()
	
	Sharpen = 1

	MotionBlur = 0

	ColorEffects[ "$pp_colour_addr" ] 		= 0
	ColorEffects[ "$pp_colour_addg" ] 		= 0
	ColorEffects[ "$pp_colour_addb" ] 		= 0
	ColorEffects[ "$pp_colour_brightness" ] 	= 0
	ColorEffects[ "$pp_colour_contrast" ] 	= 1.0
	ColorEffects[ "$pp_colour_colour" ] 		= 1.1
	ColorEffects[ "$pp_colour_mulr" ] 		= 0
	ColorEffects[ "$pp_colour_mulg" ] 		= 1
	ColorEffects[ "$pp_colour_mulb" ] 		= 1
	
end

ResetEffects()

SharpenA = 0
SharpenB = 0
BlurA = 0
BlurB = 0
BlurC = 0

function IncredibleColor()
	ColorEffects[ "$pp_colour_mulr" ] 	= math.Approach( ColorEffects[ "$pp_colour_mulr" ], 0, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_mulg" ]	= math.Approach( ColorEffects[ "$pp_colour_mulg" ], 0, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_mulb" ] 	= math.Approach( ColorEffects[ "$pp_colour_mulb" ], 0, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_contrast" ] = math.Approach( ColorEffects[ "$pp_colour_contrast" ], 1, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_colour" ] = math.Approach( ColorEffects[ "$pp_colour_colour" ], 1.1, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_addr" ] 	= math.Approach( ColorEffects[ "$pp_colour_addr" ], 0.00, FrameTime() )
	ColorEffects[ "$pp_colour_addg" ] 	= math.Approach( ColorEffects[ "$pp_colour_addg" ], 0.00, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_addb" ] 	= math.Approach( ColorEffects[ "$pp_colour_addb" ], 0.00, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_brightness" ] 	= math.Approach( ColorEffects[ "$pp_colour_brightness" ], 0.00, FrameTime() * 0.3 )
		SharpenA = math.Approach(SharpenA, 0, FrameTime())
		SharpenB = math.Approach(SharpenB, 0, FrameTime())
	DrawSharpen(SharpenA, SharpenB)
		BlurA = math.Approach(BlurA, 0, FrameTime())
		BlurB = math.Approach(BlurB, 0, FrameTime())
		BlurC = math.Approach(BlurC, 0, FrameTime())
	DrawMotionBlur(  BlurA, BlurB, BlurC )
end

function DrawDeathColor()
	ColorEffects[ "$pp_colour_colour" ] 	= math.Approach( ColorEffects[ "$pp_colour_colour" ], 3, FrameTime() * 0.3 )
	ColorEffects[ "$pp_colour_addr" ] 	= math.Approach( ColorEffects[ "$pp_colour_addr" ], 1, FrameTime() )
	DrawSharpen( 1, 0.25 )
end

function DrawClassChangeCol()
	if LocalPlayer():Team() >= 10 and LocalPlayer():Team() < 19 then
		ColorEffects[ "$pp_colour_colour" ] 	= math.Approach( ColorEffects[ "$pp_colour_colour" ], 1, FrameTime() )
		ColorEffects[ "$pp_colour_addr" ] 	= math.Approach( ColorEffects[ "$pp_colour_addr" ], 0.5, FrameTime() )
		ColorEffects[ "$pp_colour_addg" ] 	= math.Approach( ColorEffects[ "$pp_colour_addg" ], 0.5, FrameTime() )
		ColorEffects[ "$pp_colour_addb" ] 	= math.Approach( ColorEffects[ "$pp_colour_addb" ], 1, FrameTime() )
		timer.Simple(1, function() ChangedClass = false end)
	end
	
	if LocalPlayer():Team() >= 20 and LocalPlayer():Team() < 29 then
		ColorEffects[ "$pp_colour_colour" ] 	= math.Approach( ColorEffects[ "$pp_colour_colour" ], 1, FrameTime() )
		ColorEffects[ "$pp_colour_addr" ] 	= math.Approach( ColorEffects[ "$pp_colour_addr" ], 0.5, FrameTime() )
		ColorEffects[ "$pp_colour_addb" ] 	= math.Approach( ColorEffects[ "$pp_colour_addb" ], 0.5, FrameTime() )
		ColorEffects[ "$pp_colour_addg" ] 	= math.Approach( ColorEffects[ "$pp_colour_addg" ], 1, FrameTime() )
		--ColorEffects[ "$pp_colour_brightness" ] 	= math.Approach( ColorEffects[ "$pp_colour_brightness" ], -1, FrameTime() )
		timer.Simple(2, function() ChangedClass = false end)
	end
end

function DrawFeedingColor()
	SharpenA = math.Approach(SharpenA, 2, FrameTime())
	SharpenB = math.Approach(SharpenB, 1, FrameTime())
	DrawSharpen(SharpenA, SharpenB)
	ColorEffects[ "$pp_colour_colour" ] 	= math.Approach( ColorEffects[ "$pp_colour_colour" ], 1, FrameTime() )
	ColorEffects[ "$pp_colour_addr" ] 	= math.Approach( ColorEffects[ "$pp_colour_addr" ], 1, FrameTime() )
	ColorEffects[ "$pp_colour_addg" ] 	= math.Approach( ColorEffects[ "$pp_colour_addg" ], 0, FrameTime() )
	ColorEffects[ "$pp_colour_addb" ] 	= math.Approach( ColorEffects[ "$pp_colour_addb" ], 0, FrameTime() )
	--ColorEffects[ "$pp_colour_brightness" ] 	= math.Approach( ColorEffects[ "$pp_colour_brightness" ], 0.1, FrameTime() )
end

function DrawFeedeColor()
		SharpenA = math.Approach(SharpenA, 2, FrameTime())
		SharpenB = math.Approach(SharpenB, 1, FrameTime())
	DrawSharpen(SharpenA, SharpenB)
		BlurA = math.Approach(BlurA, 0.1, FrameTime())
		BlurB = math.Approach(BlurB, 0.79, FrameTime())
		BlurC = math.Approach(BlurC, 0.05, FrameTime())
	DrawMotionBlur(  BlurA, BlurB, BlurC )
	
	ColorEffects[ "$pp_colour_colour" ] 	= math.Approach( ColorEffects[ "$pp_colour_colour" ], 1, FrameTime() )
	ColorEffects[ "$pp_colour_addr" ] 	= math.Approach( ColorEffects[ "$pp_colour_addr" ], 1, FrameTime() )
	ColorEffects[ "$pp_colour_addg" ] 	= math.Approach( ColorEffects[ "$pp_colour_addg" ], 0, FrameTime() )
	ColorEffects[ "$pp_colour_addb" ] 	= math.Approach( ColorEffects[ "$pp_colour_addb" ], 0, FrameTime() )
end

local function DrawColor()
	
	if ( !LocalPlayer():Alive() and LocalPlayer() ) then
		DrawDeathColor()
	elseif ChangedClass == true and LocalPlayer():Alive() and LocalPlayer()then
		timer.Simple(1, function() DrawClassChangeCol() end)
	elseif LocalPlayer():GetNWBool("Feede") and LocalPlayer():Alive() and LocalPlayer() then
		DrawFeedeColor()
	elseif LocalPlayer():GetNWBool("Feeder") and LocalPlayer():Alive() and LocalPlayer() then
	else
		IncredibleColor();
	end
	
	DrawColorModify( ColorEffects )
	--DrawMotionBlur( 0.1, 0.5, 0.05 )
	--DrawSharpen(3, 0.5)
	--DrawBloom( 0, 0.75, 3, 3, 2, 3, 255, 0, 0 ) 
end

hook.Add( "RenderScreenspaceEffects", "RenderPostProcessing", DrawColor )

function FeedingColors()
	print("I was called")
	LocalPlayer():SetNWBool("Feede", true)
	timer.Simple(2, function() LocalPlayer():SetNWBool("Feede", false) end)
end
concommand.Add("fucking", FeedingColors)

function UpdateStatus(ply, vichealth, ownhealth, status)
	FeedeHP = vichealth
	SelfHP = ownhealth
	Feeding = status
end
